package com.sishu.websocket4.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.sishu.websocket4.pojo.TransactionRecord;
import com.sishu.websocket4.pojo.User;
import com.sishu.websocket4.service.Userservice;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user/")
@CrossOrigin(origins = "*")
public class UserController {

    @Resource
    private Userservice userService;
    @Resource
    private HttpSession httpSession;

//    @PostMapping("/register")
//    public String register(@RequestParam String username, @RequestParam String password) {
//        userService.register(username, password);
//        return "注册成功";
//    }
//
//    @PostMapping("/login")
//    public String login(@RequestParam String username, @RequestParam String password) {
//        User user = userService.login(username, password);
//        StpUtil.login(user.getUid());
//        return "登录成功";
//    }

    @RequestMapping("dologin")
    public SaResult doLogin(@Param("username") String username, @Param("password") String password, @Param("type") String type) {
        User user = userService.getUserByName(username);
        if (null == user) {
            return SaResult.error("用户不存在");
        }

        if (!type.equals(user.getUserType())) {
            return SaResult.error("用户种类出错请重新勾选");
        }
        StpUtil.login(user.getUserId());
        log.info(user.getUserId() + "登录成功");

        String token = StpUtil.getTokenValue();
        String websocketUrl = "ws://localhost:9876/websocketMessage";
        System.out.println("--------------------------------");
        return SaResult.ok("登录成功").setData(Map.of("token", token, "websocketUrl", websocketUrl));

    }

    @RequestMapping("islogin")
    @SaCheckLogin
    public SaResult isLogin() {
        return SaResult.ok("是否登录").set("isLogin", StpUtil.isLogin());
    }

    //批量添加用户
//    @PostMapping("adduser")
//    public SaResult addUser(@RequestBody List<User> users) {
//        System.out.println("users = " + users);
//        for (User user : users) {
//            userService.insertusers(user);
//            userService.insertgroups(user.getUserId(), httpSession.getId());
//            userService.insetAssets(httpSession.getId(), user.getUserId());
//        }
//        // 生成 Satoken 并返回
//        String token = StpUtil.getTokenValue();
//        return SaResult.ok("用户添加成功");
//    }

    @PostMapping("addgroup")
    public SaResult addgroup(@RequestBody List<User> users){
        System.out.println("Groups = " + users);
        //循环打印user 查找是否在数据库中

//        //当用户名和userid都相同时才能添加到数据库中
//        for (User user : users) {
//            SaResult selectuser = userService.selectuser(user);
//            if (selectuser.equals(SaResult.error(user.getUserId() + "用户不存在,或用户名不一致,无法创建小组"))) {
//                return selectuser;
//            }
//        }
         //当users对应的所有userid相同时才能添加到数据库中
        boolean flag = true;//默认所有的都在数据库中
        SaResult selectuser = SaResult.ok();
        for (User user : users) {//10001
             selectuser = userService.selectuser(user);//结果为"用户在数据库中已存在"//结果为"当前id用户不存在,无法创建小组"
            if(selectuser.getCode()==500){
                flag=false;//有一个不在数据库中就返回false
                return selectuser;
            }
//            if (selectuser.equals(SaResult.error(user.getUserId()+"用户不存在,无法创建小组"))) {
//                return selectuser;
//            }
        }

        if (selectuser.getCode()==200){
            // 数据库插入组
            SaResult insertgroups = userService.insertgroups(users, httpSession.getId());
            //     在数据库插入资产表
            for (User user : users) {
                userService.insetAssets(httpSession.getId(), user.getUserId());
            }
            return insertgroups;
        }
          return selectuser;


    }

    //查询该组的资产情况
    @RequestMapping("groupasset")
    public  SaResult selectgroupasset(@Param("groupId") String groupId) {
        return  userService.selcetgroupasset(groupId);
    }


    //根据userid查询username
    @RequestMapping("selectusername")
    public SaResult selectusername(@Param("userId") String userId) {
        String selectusername = userService.selectusername(userId);
        return SaResult.ok("ok").setData(selectusername);
    }

//    @RequestMapping("zonghejiaoyi")
//    public SaResult zonghejiaoyi(@RequestBody TransactionRecord record) {
//        System.out.println("record = " + record);
//       if (record.getTransactionType().equals("1")){
//           //如果交易的种类是(交易)
//           return  userService.transaction(record.getPayerUserId(), record.getGeterUserId(), record.getAmount());
//       }
//       if (record.getTransactionType().equals("2")){
//           //如果交易的种类是(购买地盘)
//           if (userService.isuserExists(record.getUserId())) {
//               return userService.buygroud(record.getUserId(), record.getSiteId());
//           } else
//               return SaResult.error("用户不存在");
//       }
//       if (record.getTransactionType().equals("3")){
//           //如果交易的种类是出售地盘
//           return userService.saleground(record.getUserId(), record.getSiteId());
//       }
//       if (record.getTransactionType().equals("4")){
//           //如果交易的种类是答题得款
//           return userService.answerjia(record.getUserId(), record.getAmount());
//       }
//       if (record.getTransactionType().equals("5")){
//           //如果交易的种类是答题扣款
//           return userService.answerkou(record.getUserId(), record.getAmount());
//
//       }//        userService.selectuser(userService.getUserByUserId(userId))
//        return null;
//
//    }

    @RequestMapping("jiaoyi")
    public SaResult zonghejiaoyi(@RequestBody TransactionRecord record) {
        System.out.println("Payer User ID: " + record.getPayerUserId());
        System.out.println("Getter User ID: " + record.getGeterUserId());
        System.out.println("Amount: " + record.getAmount());
        System.out.println("Transaction Type: " + record.getTransactionType());
        System.out.println("Site ID: " + record.getSiteId());
        System.out.println("Group ID: " + record.getGroupId());
        System.out.println("User ID: " + record.getUserId());
        System.out.println("Card No: " + record.getCardNo());

        if ("1".equals(record.getTransactionType())) {
            // 如果交易的种类是(交易)
            return userService.transaction(record.getPayerUserId(),record.getGeterUserId(), record.getAmount(), record.getGroupId());
        }
        else if ("2".equals(record.getTransactionType())) {
            // 如果交易的种类是(购买地盘)
            if (userService.isuserExists(record.getUserId())) {
                return userService.buygroud(record.getUserId(), record.getSiteId(), record.getGroupId());
            } else {
                return SaResult.error("用户不存在");
            }
        }
        else if  ("3".equals(record.getTransactionType())) {
            // 如果交易的种类是出售地盘
            return userService.saleground(record.getUserId(), record.getSiteId(), record.getGroupId());
        }
        else if ("4".equals(record.getTransactionType())) {
            // 如果交易的种类是答题得款
            return userService.answerjia(record.getUserId(), record.getAmount(), record.getGroupId());
        }
        else if ("5".equals(record.getTransactionType())) {
            // 如果交易的种类是答题扣款
            return userService.answerkou(record.getUserId(), record.getAmount(), record.getGroupId());
        }
        else if ("6".equals(record.getTransactionType())) {
            // 如果交易的种类是抽卡
            return userService.card(record);
        }
        else return SaResult.error("用户种类不存在");

    }


    //根据userid买地
//    @RequestMapping("buygroud")
//    public SaResult buygroud(@Param("userId") String userId, @Param("siteId") String siteId) {
//
//
//            if (userService.isuserExists(userId)) {
//                SaResult buygroud = userService.buygroud(userId, siteId);
//                return buygroud;
//            } else
//                return SaResult.error("用户不存在");
//
//    }

    //玩家a交易给玩家b
//    @RequestMapping("transaction")
//    public SaResult transaction(@Param("payeruserId") String payeruserId
//            ,@Param("geteruserId") String geteruserId,@Param("amount")int amount) {
//
//       return userService.transaction(payeruserId,geteruserId,amount);
//
//    }

    //卖地
 //   @RequestMapping("salegroud")
//    public SaResult saleground(@Param("userId") String userId
//            ,@Param("siteId") String siteId) {
//             return userService.saleground(userId,siteId);
//    }

    //答题得款
//    @RequestMapping("answerjia")
//    public SaResult answerjia(@Param("userId") String userId,@Param("amount") int amount){
//        return userService.answerjia(userId,amount);
//    }

    //答题扣款
//    @RequestMapping("answerkou")
//    public SaResult answerkou(@Param("userId") String userId,@Param("amount") int amount){
//        return userService.answerkou(userId,amount);
//    }

    @RequestMapping("tiku")
    public SaResult tiku(){
        return null;
    }




}



